package com.woniuxy.interceptor;

import com.woniuxy.annotations.RequirePerm;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @Author: 马宇航
 * @Description: 认证权限拦截器
 * @DateTime: 22/10/14/0014 15:30
 * @Component: 成都蜗牛学苑
 **/
@Component
public class AuthPermInterceptor extends HandlerInterceptorAdapter {
    /**
     * 拦截器，在进入controller方法之前进行拦截
     * ChangeLog : 1. 创建 (22/10/14/0014 15:31 [马宇航]);
     * @param request
     * @param response
     * @param handler 就是我们目标的方法！！！！！！！
     * @return boolean
    */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断这个handler是不是方法。排除直接访问静态资源的情况
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            //获取handler方法的注解，直接获取有问题，判断有没有这个注解
            if (method.isAnnotationPresent(RequirePerm.class)) {
                RequirePerm authPermAnno = method.getAnnotation(RequirePerm.class);
                //这个是注解的权限 假设是 order::all
                String permValue = authPermAnno.value();
                if ("order::all".equals(permValue)){
                    return true;
                }else {
                    return false;
                }
            }else {
               return true;
            }
        }else {
            return true;
        }
    }
}
